package com.ruoyi.web.controller.contrctrefund;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RateLimiter;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contrctrefund.domain.HouseContrctRefund;
import com.ruoyi.contrctrefund.service.IHouseContrctRefundService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 退租Controller
 * 
 * @author hct
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/contrctrefund/contrctrefund")
public class HouseContrctRefundController extends BaseController
{
    @Autowired
    private IHouseContrctRefundService houseContrctRefundService;

    /**
     * 查询退租列表
     */
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseContrctRefund houseContrctRefund)
    {
        startPage();
        List<HouseContrctRefund> list = houseContrctRefundService.selectHouseContrctRefundList(houseContrctRefund);
        return getDataTable(list);
    }

    /**
     * 导出退租列表
     */
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:export')")
    @Log(title = "退租", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseContrctRefund houseContrctRefund)
    {
        List<HouseContrctRefund> list = houseContrctRefundService.selectHouseContrctRefundList(houseContrctRefund);
        ExcelUtil<HouseContrctRefund> util = new ExcelUtil<HouseContrctRefund>(HouseContrctRefund.class);
        util.exportExcel(response, list, "退租数据");
    }

    /**
     * 获取退租详细信息
     */
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseContrctRefundService.selectHouseContrctRefundById(id));
    }

    /**
     * 新增退租
     */
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:add')")
    @Log(title = "退租", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseContrctRefund houseContrctRefund)
    {
        houseContrctRefund.setCreateBy(getUsername());
        houseContrctRefund.setUpdateBy(getUsername());
        return toAjax(houseContrctRefundService.insertHouseContrctRefund(houseContrctRefund,getUserId(),getUsername()));
    }

    /**
     * 修改退租
     */
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:edit')")
    @Log(title = "退租", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseContrctRefund houseContrctRefund)
    {
        return toAjax(houseContrctRefundService.updateHouseContrctRefund(houseContrctRefund));
    }

    /**
     * 删除退租
     */
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:remove')")
    @Log(title = "退租", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseContrctRefundService.deleteHouseContrctRefundByIds(ids));
    }


    /**
     * 终止租赁+结算剩余租金接口
     * 1.修改合同状态
     * 2.创建完结单
     * 3.修改房间状态
     * 4.结算结余账单金额并支付
     * 5.记录支付支出项
     * @param houseContrctRefund
     * @return
     */
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:finish')")
    @Log(title = "终止合同", businessType = BusinessType.INSERT)
    @PostMapping("/finish")
    public AjaxResult finishContrct(HouseContrctRefund houseContrctRefund){
        Long userId = getUserId();
        try {
            Object o = houseContrctRefundService.finishContrct(houseContrctRefund, userId);
            return AjaxResult.success("终止合同成功",o);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 退租审批
     * 1.创建退租单
     * 2.修改房间状态
     * 3.修改账单状态
     * @param refundId
     * @return
     */
    @RateLimiter(time = 1,count = 1)
    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:finish')")
    @Log(title = "终止合同", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/apply/{refundId}")
    public AjaxResult applyContrctrefund(@PathVariable Long refundId){
        Long userId = getUserId();
        String username = getUsername();
        try {
            Object o = houseContrctRefundService.confirmContrctRefund(refundId, userId, username);
            return AjaxResult.success("结算单审批完成",o);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }



    @PreAuthorize("@ss.hasPermi('contrctrefund:contrctrefund:info')")
    @PostMapping(value = "/allData/{refundId}")
    public AjaxResult getContrctrefundAllData(@PathVariable Long refundId){
        try {
            Object contrctRefundAllData = houseContrctRefundService.getContrctRefundAllData(refundId);
            return AjaxResult.success("退单详情",contrctRefundAllData);
        } catch (Exception e) {
            return AjaxResult.error("查询失败");
        }
    }
}
